if __name__ == '__main__':
    pass

import pymongo

class Games():
    def __init__(self, connection):
        self.db = connection
        
    def write_stage_line(self, line):
        #game_id = line['id']
        #line.pop('id', None)
        self.db.insert(line)

    def read_stage_line(self, id):
        return self.db.find_one({'hid': '{}'.format(id)})



class ID_Cache():
    
    def __init__(self, connection):
        self.db = connection

    def write_opposition(self):
        from db import DB_Agent

        mysql = DB_Agent()

        mysql.connect()
        res  = mysql.execute('select distinct(nid), field_teamshortname_value, field_teamshortname_original_value, field_teamname_value, field_teamname_original_value from content_type_team_profile;', \
                             silent=True)

        for line in res:
            team_info = {}
            team_info['b_id'] = line[0]
            team_info['b_short_name_russian']   = line[1]
            team_info['b_short_name_original']  = line[2]
            team_info['b_full_name_russian']    = line[3]
            team_info['b_full_name_original']   = line[4]

            print (team_info, self.db.opposition.count())
            self.db.opposition.insert(team_info)

        mysql.close()

    def opposition_id(self, opposition_short_name):
        return self.db.opposition.find_one({'b_short_name_original': opposition_short_name})['b_id']

    def write_tournaments(self):
        tourn_dict = { 'European Cup Winners':    4653,\
                            '1st Division':            4654,\
                            '2nd Division':            4655,\
                            'Centenary Trophy':        5195,\
                            'Champions League':        310,\
                            #'Champions L.':        310,\
                            'CL ':                  310,\
                            'Charity Shield':         4672,\
                            'Community Shield':        4673,\
                            'Europa League':        3243,\
                            'European Cup':            4675,\
                            'European Fairs Cup':    4676,\
                            'European Super Cup':    4677,\
                            'FA Cup'            :    154,\
                            'Lancashire League':    4678,\
                            'League Cup':            319,\
                            'Premier League':        69,\
                            'Screen Sport':            4679,\
                            'Sheriff of London':    4674,\
                            'Test Match':            4680,\
                            'UEFA Cup':                4682,\
                            'World Club':            4681
                         }

        self.db.insert({"tournament": tourn_dict})

  
    def tournament_id(self, tournament_name):
        return self.db.tournament.find_one({'h_name': tournament_name})['b_id']



connection = pymongo.MongoClient("localhost", 27017).test

cache = ID_Cache(connection.id)
#id.write_opposition()
#cache.write_tournaments()

print(cache.tournament_id('World Club'))
print(cache.opposition_id('Everton'))


club_rus_name = {'Real Madrid':'Реал Мадрид', 'Liverpool': 'Ливерпуль', 'Ludo Razgd': 'Людогорец', 'FC Basel': 'Базель',
                                'Juventus': 'Ювентус', 'Atl Madrid': 'Атлетико Мадрид', 'Malmö FF': 'Мальмо', 'Olympiakos': 'Олимпиакос',
                                'Zenit St P':'Зенит', 'Monaco': 'Монако', 'Bayer Levkn': 'Байер', 'Benfica': 'Бенфика',
                                'Bor Dortmd': 'Боруссия', 'Arsenal': 'Арсенал', 'Anderlecht': 'Андерлехт', 'Galatasaray': 'Галатасарай',
                                'Bayern Mun': 'Бавария', 'Roma': 'Рома', 'Man City': 'Ман. Сити', 'CSKA': 'ЦСКА',
                                'Paris St G': 'ПСЖ', 'Barcelona': 'Барселона', 'Ajax': 'Аякс', 'Apoel Nic': 'Апоэль',
                                'Chelsea': 'Челси', 'NK Maribor': 'Марибор', 'Schalke': 'Шальке', 'Sporting': 'Спортинг',
                                'FC Porto': 'Порто',  'BATE Bor': 'БАТЭ', 'Shakt Donsk': 'Шахтер', 'Ath Bilbao': 'Атлетик Бильбао'}



